<?php 
class Filters {
	
	private $__input = null;
	private $__output = null;
	
	public function __call($name, $arguments) {
		if (!method_exists($this, $name)) {
			return false;
		}
		
		$input = WWW_ROOT . str_replace('/', DS, FILTERS_DIR) . (isset($arguments[0]) ? $arguments[0] : '');
		$output = WWW_ROOT . str_replace('/', DS, FILTERS_DIR) . $name . '.jpg';
		
		if (!is_file($input)) {
			return false;
		}
		
		//if (!is_file($output)) {
			copy($input, $output);
			$this->{$name}($output);
		//}
		
		return true;
	}
	
	private function execute($command) {
		// remove newlines and convert single quotes to double to prevent errors
		$command = str_replace(array("\n", "'"), array('', '"'), $command);
		// replace multiple spaces with one
		$command = preg_replace('#(\s){2,}#is', ' ', $command);
		// escape shell metacharacters
		$command = escapeshellcmd($command);
		// execute convert program
		exec($command);
	}
	
	private function overlay($input, $mask, $alpha, $type = 'jpeg', $width = 600, $height = 600) {
		$dest = imagecreatefromjpeg($input);
		
		if (strtoupper($type) == 'PNG') {
			$src = imagecreatefrompng($mask);
		} else {
			$src = imagecreatefromjpeg($mask);
		}
		
		imagecopymerge($dest, $src, 0, 0, 0, 0, $width, $height, $alpha);
		imagejpeg($dest, $input);
	}
	
	private function composite($input, $mask, $alpha = '50', $mode = 'Multiply') {
		$tmp =  WWW_ROOT . str_replace('/', DS, FILTERS_DIR) . uniqid('', true) . '.png';
		$this->execute("convert $mask -alpha opaque -channel a -evaluate set $alpha% +channel $tmp");
		$this->execute("convert $input $tmp -compose $mode -composite $input");
		unlink($tmp);
	}
	
	private function alpha($input, $alpha, $type = 'jpeg', $width = 600, $height = 600) {
		$tmp =  WWW_ROOT . str_replace('/', DS, FILTERS_DIR) . uniqid('', true) . '.jpg';
		copy($input, $tmp);
		$this->execute("convert $tmp -channel A -evaluate 50% $tmp");
		return $tmp;
	}
	
	private function thumbnail($input, $size = '120x120') {
		$this->execute("convert $input -resize $size $input");
	}
	
	private function beauty($input) {
		$this->execute("convert $input -color-matrix '1.3 0 0 0   0 1.3 0 0   0 0 1.3 0   0 0 0 1' $input");
	}
	
	private function bright($input) {
		$this->execute("convert $input -color-matrix '1.3 0 0 0   0 1.3 0 0   0 0 1.3 0   0 0 0 1' $input");
		$this->execute("convert $input -brightness-contrast '10, 12' $input");
	}
	
	private function brownish($input) {
		$this->execute("convert $input -color-matrix '1 .2 0 0   .1 1 0 0   0 0 1 0   0 0 0 1' $input");
		$mask = WWW_ROOT . str_replace('/', DS, MASKS_DIR) . 'brownish_overlay_0.5.jpg';
		$this->composite($input, $mask, 50, 'Overlay');
	}
	
	private function chrome($input) {
		$this->execute("convert $input -color-matrix '.35 .35 .35 0   .35 .35 .35 0   .35 .35 .35 0   0 0 0 1' $input");
		$this->execute("convert $input -brightness-contrast '-15.68, 12' $input");
		$mask = WWW_ROOT . str_replace('/', DS, MASKS_DIR) . 'chrome_softlight_1.0.png';
		$this->composite($input, $mask, 100, 'softlight');
	}
	
	private function griseous($input) {
		$this->execute("convert $input -color-matrix '1 0 0 0   0 1 .1 0   0 .2 1 0   0 0 0 1' $input");
		$this->execute("convert $input -set option:modulate:colorspace HSB -modulate '90, 50' $input");
		$this->execute("convert $input -brightness-contrast '0, 15' $input");
		$mask = WWW_ROOT . str_replace('/', DS, MASKS_DIR) . 'griseuos_multiply_0.3.jpg';
		$this->composite($input, $mask, 50);
	}
	
	private function happytear($input) {
		$mask = WWW_ROOT . str_replace('/', DS, MASKS_DIR) . 'happytear_overlay_0.45.png';
		$this->composite($input, $mask, 45, 'Overlay');
	}
	
	private function inkwash($input) {
		$this->execute("convert $input -color-matrix '.25 .25 .25 0   .25 .25 .25 0   .35 .3 .3 0   0 0 0 1' $input");
		$this->execute("convert $input -brightness-contrast '0, 6' $input");
		$mask = WWW_ROOT . str_replace('/', DS, MASKS_DIR) . 'inkwash_overlay_0.8.jpg';
		$this->composite($input, $mask, 80, 'Overlay');
	}
	
	private function instant($input) {
		$this->execute("convert $input -set option:modulate:colorspace HSB -modulate '100, 50' $input");
		$this->execute("convert $input -brightness-contrast '0, 15' $input");
		$this->execute("convert $input -color-matrix '1 0 0 0   .03 1 .03 0   0 0 1 0   0 0 0 1' $input");
		$mask = WWW_ROOT . str_replace('/', DS, MASKS_DIR) . 'instant_multiply_0.6.jpg';
		$this->composite($input, $mask, 60);
	}
	
	private function lomo($input) {
		$this->execute("convert $input -color-matrix '1.2 0 0 -0.1   0 1.2 0 -0.1   0 0 1.2 -0.1   0 0 0 1' $input");
		$this->execute("convert $input -modulate 80, 100, 105 $input");
		$mask = WWW_ROOT . str_replace('/', DS, MASKS_DIR) . 'lomo_overlay_1.0.jpg';
		$this->execute("convert $input $mask -compose Overlay -composite $input");
	}
	
	private function nostalgia($input) {
		$mask1 = WWW_ROOT . str_replace('/', DS, MASKS_DIR) . 'nostalgia_sortlight_0.7.png';
		$mask2 = WWW_ROOT . str_replace('/', DS, MASKS_DIR) . 'nostalgia_multiply_1.0.jpg';
		$this->composite($input, $mask1, 70, 'softlight');
		$this->execute("convert $input $mask2 -compose Multiply -composite $input");
		$this->execute("convert $input -set option:modulate:colorspace HSB -modulate '120' $input");
	}
	
	private function retro($input) {
		
		$this->execute("convert $input -set option:modulate:colorspace HSB -modulate '70, 50' $input");
		$mask = WWW_ROOT . str_replace('/', DS, MASKS_DIR) . 'retro_multiply_0.7.jpg';
		$tmp =  WWW_ROOT . str_replace('/', DS, FILTERS_DIR) . uniqid('', true) . '.png';
// 		$this->execute("convert ( $input -set option:modulate:colorspace HSB -modulate '100, 85' ) $mask -compose dissolve -define compose:args=30 -composite -set option:modulate:colorspace HSB -modulate '100' $input");
		$this->composite($input, $mask, 70);
		$this->execute("convert $input -set option:modulate:colorspace HSB -modulate '150' $input");
	}
	
	private function richtone($input) {
		$this->execute("convert $input -color-matrix '1.2 0 0 0   0 1.25 0 0   0 0 1 0   0 0 0 1' $input");
		$mask = WWW_ROOT . str_replace('/', DS, MASKS_DIR) . 'richtone_colorburn_0.5.jpg';
		$this->composite($input, $mask, 50, 'color-burn');
	}
	
	private function sunrise($input) {
		$this->execute("convert $input -color-matrix '1 .08 .08 .08   0 1 0 0   0 0 1 0   0 0 0 1' $input");
		$this->execute("convert $input -set option:modulate:colorspace HSB -modulate '110, 80' $input");
	}
	
	private function vibrant($input) {
		$this->execute("convert $input -color-matrix '1.5 0 0 .1   0 1.5 0 .1   0 0 1.5 .1   0 0 0 1' $input");
		$this->execute("convert $input -brightness-contrast '-15, 10' $input");
	}
	
	private function xpro($input) {
		$this->execute("convert $input -color-matrix '1.5 0 0 .1   0 1.5 0 .1   0 0 1.5 .1   0 0 0 1' $input");
		$this->execute("convert $input -set option:modulate:colorspace HSB -modulate '85, 120, 105' $input");
		$mask = WWW_ROOT . str_replace('/', DS, MASKS_DIR) . 'xpro_multiply_0.8.jpg';
		$this->composite($input, $mask, 80);
	}
	
	public function extract($input, $output, $x = 0, $y = 0, $size=600) {
		$this->execute("convert -extract '{$size}x{$size}+{$x}+{$y}' $input -resize '600x600' $output");
	}
	
	public function createFilterButtons($input) {
		
	}

}
